# Maintainer: Sutou Kouhei <kou@clear-code.com>

_realname=gr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.73.7
pkgrel=2
pkgdesc="A graphics library for visualisation applications (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gr-framework.org/"
msys2_repository_url="https://github.com/sciapp/gr"
msys2_references=(
  'aur: gr-framework'
)
license=("spdx:MIT")
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-glfw"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-qhull"
         "${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zeromq")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/sciapp/gr/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('2584727b1413a337ef14daae1e61bdc5c946403031695b42ecfbf8bc1888d132')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  # We can't use bsdtar to extract the archive because the archive includes
  # symbolic link.
  rm -rf "${_realname}-${pkgver}"
  tar -xf "${_realname}-${pkgver}.tar.gz" || true

  cd "${srcdir}/${_realname}-${pkgver}"

  # Set version manually, otherwise it resets to zero
  echo "${pkgver}" > version.txt
  sed -i "s|\${GR_VERSION}|${pkgver}|g" CMakeLists.txt
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DGR_USE_BUNDLED_LIBRARIES=OFF \
    "${_extra_config[@]}" \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.md "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}
